﻿#include "mainwindow.h"

#include <QApplication>
#include <QDataStream>
#include <QDebug>

#include <person.h>
#include <Function.h>
#include <queen.h>

//类模版的静态成员初始化
//静态成员初始化只能在函数体外
template <typename T>
int Person<T>::count=0;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    //指定模版类型
    Person<double> xiaoyan("xiaoyan",17,8); //萧炎

    {
        //加了大括号之后，都是xiaoyan，但是不冲突，
        //括号内的xiaoyan与括号外的无关系
        Person<double> xiaoyan("xiaoyan2",18,9); //萧炎
    }

    test_survival_period();

    Person<double> xuner("xuner", 16, 15);

    qDebug() << "萧炎目前的斗之气等级 " << xiaoyan.level;
    level_up(xiaoyan);
    qDebug() << "升级后的斗之气等级 " << xiaoyan.level;

    //使用重载函数，连升3级别
    level_up(xiaoyan,3);
    qDebug() << "升级后的斗之气等级 " << xiaoyan.level;

    Monster snake; //岩蛇
    snake.name = "snake";
    snake.level = 1;

    qDebug() << "岩蛇目前的斗之气等级 " << snake.level;
    level_up(snake);
    qDebug() << "岩蛇升级后的斗之气等级 " << snake.level;
    //指定模版类型
    bool outcome = xiaoyan.fight<double>(snake);

    qDebug() << "萧炎与岩蛇的战斗结果 " << outcome;

    qDebug() << "萧炎目前的斗技 " << xiaoyan.skill.name;
    //斗技
    Skill skill;
    skill.name = "八级崩";

    //赋值给萧炎新的斗技
    xiaoyan.skill = skill;

    qDebug() << "萧炎目前的斗技 " << xiaoyan.skill.name;


    //这里把level_up的参数改成了常引用，
    //试试编译有报错吗？

    //这里定义一个常对象
    const bool victory = true;
    //修改victory看看编译有报错吗？
    //victory = false;

    //使用数组
    //Person<double>* person_set[3];
    //Skill skill_set[3];

    Skill skill_1("吸掌");
    Skill skill_2("八级崩");
    Skill skill_3("爆步");

    /*
    skill_set[0] = skill_1;
    skill_set[1] = skill_2;
    skill_set[2] = skill_3;

    for(int i=0; i<3; i++){
        qDebug() << "i " << i
                 << skill_set[i].name;
    }
    */

    //数组初始化
    Skill skill_set[3] = {skill_1 , skill_2, skill_3};

    for(int i=0; i<3; i++){
        qDebug() << "i " << i
                 << skill_set[i].name;
    }

    //数组作为参数
    print_all_skill(skill_set, 3);

    //非指针类型对象
    Person<double> Frank("Frank",35,9);
    //指针类型对象
    Person<double>* Yao_lao = new Person<double>("yaolao",45,88);

    qDebug() << " Frank is Pointer ? " << isPointer(Frank);
    //使用&将普通对象转为指针类型。
    Person<double>* Frank_copy = &Frank;
    qDebug() << " Frank_copy is Pointer ? " << isPointer(Frank_copy);

    qDebug() << " Yao_lao is Pointer ? " << isPointer(Yao_lao);
    //使用*转换为普通对象
    qDebug() << " Yao_lao is Pointer ? " << isPointer(*Yao_lao);


    //使用自增和自减操作指针
    Skill* p = skill_set;
    qDebug() << "p "  << p->name;
    p++;
    qDebug() << "p "  << p->name;
    p++;
    qDebug() << "p "  << p->name;

    //定义一个指针数组
    Person<double>* person_set[3];
    person_set[0] = new Person<double>("小医仙",18,6);
    person_set[1] = new Person<double>("萧鼎",19,16);
    person_set[2] = new Person<double>("萧厉",18,15);

    qDebug() << person_set[0]->name << " level " << person_set[0]->level;
    qDebug() << person_set[1]->name << " level " << person_set[1]->level;
    qDebug() << person_set[2]->name << " level " << person_set[2]->level;

    //函数名为指针,
    //定义指针的类型为：bool can_find_fire(Person<double> * person)
    // 使用std::function声明一个函数指针
    std::function<bool(Person<double>*)> func = can_find_fire;

    //调用func
    func(person_set[0]);

    //释放指针占用的内存
    delete person_set[0];
    delete person_set[1];
    delete person_set[2];

    //创建女王对象
    Queen meidusha;
    //女王调用远古灵蛇的斗技
    meidusha.use_ancient_fighting_skill();
    //创建怪物类
    Monster monkey;
    monkey.name = "岩猿";
    monkey.level = 1;
    //女王调用人类的函数打怪兽
    meidusha.fight<double>(monkey);

    //调用与父类相同的成员函数
    meidusha.use_ancient_fighting_skill();
    //调用与父类相同的成员变量
    qDebug() << " meidusha.name " << meidusha.name;
    //调用自己的函数
    meidusha.use_secret_skill();

    //看看编译会不会报错，美杜莎能否使用远古灵蛇的private属性函数
    //meidusha.use_ancient_unique_skill();

    //美杜莎使用与远古天蛇同名的函数
    meidusha.AncientSnake::use_ancient_fighting_skill();

    //
    //meidusha.grow_up();
    //meidusha.Person<double>::grow_up();
    //meidusha.AncientSnake::grow_up();
    //meidusha.born_place;

    //赋值兼容
    //Creature father_1 = meidusha; //看看是否可以编译通过
    Creature * father_2 = &meidusha; //看看是否可以编译通过
    Creature & father_3 = meidusha; //看看是否可以编译通过

    //father_1.grow_up();
    father_2->grow_up();
    father_3.grow_up();
    //father_3.use_secret_skill();  //看看是否可以编译通过

    //使用自定义的运算符
    xiaoyan++;

    //使用try...catch...捕获异常
    try{
        //美杜莎低于6级，则转换人型会失败，并抛出异常
        meidusha.change_to_person(5);
    }
    catch(bool state){
        qDebug() << "catch state " << state;
    }
    return a.exec();
}
